﻿using System.Reflection;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.WowAddonStudio.Setup.CustomActions;
using NUnit.Framework;
using System.IO;
using System;

namespace WowAddonStudio.Setup.CustomActions.Tests
{
    [TestFixture]
    public class CustomActionTest
    {
        private const string filePath = @"Interface\FrameXML\UI.xsd";

        private MpqExtractor mpqExtractor;

        [SetUp]
        public void SetUp()
        {
            mpqExtractor = new MpqExtractor(new MockSession());
        }

        [Test]
        public void GetLocalesTest()
        {
            string[] locales = WowManager.GetLocales();

            Assert.IsNotNull(locales);
            Assert.AreNotEqual(0, locales.Length, "No locales returned by MpqExtractor.");

            foreach (var locale in locales)
            {
                if (locale.IndexOf(Path.DirectorySeparatorChar) != -1)
                    Assert.Fail("Locales cannot be full paths.");
            }
        }

        [Test]
        public void GetLocalizedMpqPathTest()
        {
            string localizedMpqPath = WowManager.GetLocalizedMpqPath("locale", "enGB");

            Assert.IsNotNull(localizedMpqPath);
        }

        [Test]
        public void ExtractFromMpqTest()
        {
            string[] locales = WowManager.GetLocales();

            if (locales == null || locales.Length == 0)
                Assert.Fail("No locales returned by MpqExtractor.");

            string localizedMpqPath = WowManager.GetLocalizedMpqPath("locale", locales[0]);
            string tempFolder = Path.GetTempPath();

            mpqExtractor.ExtractFromMpq(localizedMpqPath, filePath, tempFolder);

            Assert.IsTrue(File.Exists(Path.Combine(tempFolder, Path.GetFileName(filePath))));
        }

        [Test]
        public void GetVisualStudioInstallDirTest()
        {
            var visualStudioInstallDir = (string)typeof (CustomAction).InvokeMember("GetVisualStudioInstallDir",
                    BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, null);

            Assert.IsNotNull(visualStudioInstallDir, "Could not detect Visual Studio installation.");
        }
    }
}